//
//  SplashVC.m
//  CJModileAdDemo
//
//  Created by CJ on 2022/3/7.
//

#import "SplashVC.h"
#import "CustomShowView.h"
#import <AMPSAdSDK/AMPSAdSDK.h>

@interface SplashVC ()<AMPSSplashAdDelegate>

@property (nonatomic, strong) AMPSSplashAd *splash;

@end

@implementation SplashVC

@synthesize customView = _customView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)dealloc
{
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)setupUI {
    self.title = @"SplashAd";
    [self.view addSubview:self.customView];
    self.customView.frame = CGRectMake(0, 100, 200, 200);
    self.customView.center = self.view.center;
}

- (void)handleEvent:(NSInteger)eve {
    if (eve == 0) {
        NSLog(@"事件:%s", __FUNCTION__);
        [self loadAdData];
        return;
    }
    [self.splash showSplashViewInWindow:[UIApplication sharedApplication].keyWindow];
}

- (CustomShowView *)customView {
    if (!_customView) {
        _customView = [[CustomShowView alloc] init];
        __weak SplashVC *weakSelf = self;
        _customView.event = ^(NSInteger eve) {
            [weakSelf handleEvent:eve];
        };
    }
    return _customView;
}

- (void)loadAdData {
    [self.splash loadSplashAd];
}

- (AMPSSplashAd *)splash {
    if (!_splash) {
        AMPSAdConfiguration *cfg = [[AMPSAdConfiguration alloc] init];
        _splash = [[AMPSSplashAd alloc] initWithSpaceId:@"124541" adConfiguration:cfg];
        _splash.delegate = self;
    }
    return _splash;
}

#pragma mark AMPSSplashAdDelegate
- (void)ampsSplashAdLoadSuccess:(AMPSSplashAd *)splashAd {
    if ([self.splash isReadyAd]) {
        [self.splash showSplashViewInWindow:[UIApplication sharedApplication].keyWindow];
    }
    NSLog(@"事件:%s", __FUNCTION__);
    for (AMPSAdLoadedInfo *info in self.splash.failAdInfoList.copy) {
        NSLog(@"事件：----失败渠道：%@，原因：%@",info.adapterClassName, info.errorMsg);
    }
}
- (void)ampsSplashAdLoadFail:(AMPSSplashAd *)splashAd error:(NSError *_Nullable)error {
    NSLog(@"事件:%s----%@", __FUNCTION__, error.debugDescription);
}
- (void)ampsSplashAdDidShow:(AMPSSplashAd *)splashAd {
    NSLog(@"事件:%s", __FUNCTION__);
}
- (void)ampsSplashAdExposured:(AMPSSplashAd *)splashAd {
    NSLog(@"事件:%s", __FUNCTION__);
}
- (void)ampsSplashAdDidClick:(AMPSSplashAd *)splashAd {
    NSLog(@"事件:%s", __FUNCTION__);
}
- (void)ampsSplashAdDidClose:(AMPSSplashAd *)splashAd {
    NSLog(@"事件:%s", __FUNCTION__);
}

@end
